package com.TeacherInfoServer.common.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.exception.RecordExistDetailException;
import com.TeacherInfoServer.mapper.TemplateFileMapper;
import com.TeacherInfoServer.pojo.DTO.AddTemplateFile;
import com.TeacherInfoServer.pojo.DTO.UpdateTemplateFile;
import com.TeacherInfoServer.pojo.entity.TemplateFile;
import com.TeacherInfoServer.pojo.query.TemplateFileQuery;
import com.TeacherInfoServer.pojo.vo.TemplateDetailVo;
import com.TeacherInfoServer.pojo.vo.TemplateListVo;
import com.TeacherInfoServer.pojo.vo.TemplateRoughlyVo;
import com.TeacherInfoServer.common.service.TemplateFileService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Lenovo
* @description 针对表【template_file】的数据库操作Service实现
* @createDate 2024-05-21 18:02:53
*/
@Service
public class TemplateFileServiceImpl extends ServiceImpl<TemplateFileMapper, TemplateFile>
    implements TemplateFileService{

    @Autowired
    TemplateFileMapper templateFileMapper;
    @Override
    public Result<?> getListRoughly(TemplateFileQuery templateFileQuery) {
        Page<TemplateRoughlyVo> page = new Page<>(templateFileQuery.getCurrentPage(), templateFileQuery.getPageSize());
        Page<TemplateRoughlyVo> result = templateFileMapper.getList(page, templateFileQuery);
        System.out.println(result.getRecords());
        TemplateListVo templateListVoList =new TemplateListVo();
        templateListVoList.setPageSize(result.getSize());
        templateListVoList.setCurrentPage(result.getCurrent());
        templateListVoList.setTotalSize(result.getTotal());
        templateListVoList.setTemplateRoughlyVoList(result.getRecords());
        return Result.success(templateListVoList);
    }
    @Override
    public Result<?> getDetails(String id){
        TemplateFile templateFile = templateFileMapper.selectById(id);
        TemplateDetailVo templateDetailVo = new TemplateDetailVo();
        BeanUtil.copyProperties(templateFile,templateDetailVo);
        return Result.success(templateDetailVo);
    }
    @Override
    @Transactional
    public Result addOne(AddTemplateFile addTemplateFile){
        TemplateFile templateFile = new TemplateFile();
        BeanUtil.copyProperties(addTemplateFile,templateFile);
        QueryWrapper<TemplateFile> queryWrapper = new QueryWrapper<>();
        if(templateFileMapper.selectOne(queryWrapper.lambda().eq(TemplateFile::getTemplateTypeName,templateFile.getTemplateTypeName()))==null)
        {
            templateFileMapper.insert(templateFile);
            return Result.success("添加成功");
        }else {
            throw new RecordExistDetailException("此模板类型已存在");
        }

    }
    @Override
    @Transactional
    public Result update(UpdateTemplateFile updateTemplateFile){
        TemplateFile templateFile = new TemplateFile();
        BeanUtil.copyProperties(updateTemplateFile,templateFile);
        templateFileMapper.updateById(templateFile);
        QueryWrapper<TemplateFile> queryWrapper = new QueryWrapper<>();
        if(templateFileMapper.selectList(queryWrapper.lambda().eq(TemplateFile::getTemplateTypeName,templateFile.getTemplateTypeName())).size()>1){
            throw new RecordExistDetailException("此模板类型已存在");
        }else{
            return Result.success("修改成功");
        }
    }

    @Transactional
    @Override
    public Result<String> deleteByIds(List<Integer> list) {
        templateFileMapper.deleteBatchIds(list);
        return Result.success("删除成功");
    }

}




